package com.wanmait.recruitment.controller;

import com.github.pagehelper.PageInfo;
import com.wanmait.recruitment.pojo.Tags;
import com.wanmait.recruitment.service.ConfigService;
import com.wanmait.recruitment.service.TagsService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("manage/tags")
public class TagsListController {
    @Resource
    private TagsService tagsService;
    @Resource
    private ConfigService configService;
    @RequestMapping("list")
    public ModelAndView list(){
        ModelAndView modelAndView = new ModelAndView();
        List<Tags> tags = tagsService.findType();
        modelAndView.addObject("tags",tags);
        return modelAndView;
    }
    @RequestMapping("date")
    public ModelAndView date(Tags tag,String pageNumber){
        Tags parent = new Tags();
        parent.setId(tag.getId());
        parent.setTagName(tag.getTagName());
        tag.setParent(parent);
        ModelAndView modelAndView = new ModelAndView();
        int pageSize;
        try {
            pageSize = Integer.parseInt(configService.getValueByKey("tags_config"));
        } catch (Exception e) {
            pageSize = Integer.parseInt(configService.getDefaultValueByKey("tags_config"));
        }
        int pageNum;
        try {
            pageNum = Integer.parseInt(pageNumber);
        } catch (NumberFormatException e) {
            pageNum = 1;
        }
        PageInfo<Tags> tagsPageInfo = tagsService.findPage(tag,pageNum,pageSize);
        modelAndView.addObject("tagsPageInfo",tagsPageInfo);
        return modelAndView;
    }
    @RequestMapping("insert")
    @ResponseBody
    public void insert(Tags tags){
        Tags parent = new Tags();
        parent.setId(tags.getId());
        tags.setParent(parent);
        tagsService.insert(tags);
    }
    @RequestMapping("update")
    @ResponseBody
    public void update(Tags tags){
        tagsService.update(tags);
    }
    @RequestMapping("del")
    @ResponseBody
    public void del(String  id){
        tagsService.delete(Integer.parseInt(id));
    }
    @RequestMapping("batchDel")
    @ResponseBody
    public void batchDel(String  idArr){
        tagsService.delete(idArr);
    }
}
